package com.arcbees.test.collaborative.client.diffsync;

import com.arcbees.test.collaborative.client.util.ClientLogger;
import com.arcbees.test.collaborative.shared.AdaptativeTimer;
import com.arcbees.test.collaborative.shared.diffsync.DiffHandler;
import com.arcbees.test.collaborative.shared.diffsync.DocumentShadow;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;

public class TimedDifferentialSync extends AbstractDifferentialSync {

  private AdaptativeTimer timer;

  @Inject
  public TimedDifferentialSync(DiffHandler diffHandler,
      DocumentShadow docShadow, ClientLogger logger,
      @Named("EditsTimerMin") int timerMin,
      @Named("EditsTimerMax") int timerMax,
      @Assisted DifferentialSyncCallback callback) {
    super(diffHandler, docShadow, logger, callback);

    timer = new AdaptativeTimer(timerMin, timerMax) {
      @Override
      public void timerTick() {
        sendEdits();
      }
    };

    timer.start();
  }

  @Override
  protected void editsAdded() {
  }

  private void sendEdits() {
    synchronize();
  }
}
